package com.xjcs.saplingplan.const

import android.content.Context
import android.graphics.Color
import android.view.View
import android.widget.ImageView
import androidx.annotation.DrawableRes
import com.blankj.utilcode.util.ToastUtils
import com.xjcs.saplingplan.R
import com.xjcs.saplingplan.utils.GlideUtils
import java.time.LocalDate
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter

fun View.setSingleClick(interval: Long = 500, action: (view: View) -> Unit) {
    setOnClickListener {
        val currentTime = System.currentTimeMillis()
        if (currentTime - (it.getTag(R.id.last_click_time) as? Long ?: 0) > interval) {
            it.setTag(R.id.last_click_time, currentTime)
            action(it)
        }
    }
}

fun Int.addZero(): String {
    return if(this < 10) {
        "0$this"
    } else {
        "$this"
    }
}

fun String.toColor(): Int {
    return Color.parseColor(this)
}

fun LocalDate.toStr(pattern: String = "yyyy-MM-dd"): String {
    return this.format(DateTimeFormatter.ofPattern(pattern))
}

fun LocalDate.timeMillis(): Long {
    return atStartOfDay().atZone(ZoneOffset.UTC).toInstant().toEpochMilli()
}

fun String.toToast() {
    ToastUtils.showShort(this)
}

// 在 Extensions.kt 中添加
fun Int.dpToPx(context: Context): Int {
    return (this * context.resources.displayMetrics.density).toInt()
}

fun ImageView.loadImage(@DrawableRes resId: Int) {
    GlideUtils.loadSimple(context, resId, this)
}

fun ImageView.loadImage(url: String?) {
    GlideUtils.loadUrlSimple(context, url, this)
}

fun ImageView.loadCircleImage(@DrawableRes resId: Int) {
    GlideUtils.loadCircleSimple(context, resId, this)
}